home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5570 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: rcp6.elan.af.mil!rscernix!danpop
  2. From: danpop@mail.cern.ch (Dan Pop)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: What to do when feof() is NOT feof()
  5. Date: 19 Feb 96 22:28:56 GMT
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <danpop.824768936@rscernix>
  8. References: <824554814snz@metsys.demon.co.uk> <4g4vpp$52f@spectator.cris.com> <danpop.824614833@rscernix> <4g7rsj$fnf@spectator.cris.com> <4ga195$nsq@lodur.easysoft.com>
  9. NNTP-Posting-Host: ues5.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11.  
  12. In <4ga195$nsq@lodur.easysoft.com> nick@news.easysoft.com (Nick Gorham) writes:
  13.  
  14. >thinks he asked, but that is not Dan's fault. As the above _Expert_
  15. >knows "rt" opens a text file on MSDOS but I don't see any mention of
  16. >it under several OS's that have 'C' compilors.
  17.  
  18. For the simple reason that there is no "rt" mode in the C language.
  19. However, "rt" is guaranteed to work everywhere and open the file in text
  20. mode.  Why?  Because the standard says that 'r' followed by any amount
  21. of _meaningless_ gibberish is the same as "r", which means: open in text
  22. mode for reading.
  23.  
  24. Then, why is "rt" documented by many MSDOS C implementations?  Because
  25. those implementations add a (brain-dead) "feature": they allow the 
  26. programmer to alter the behaviour of the stdio library so that "r"
  27. actually means "rb".  Needless to say, once you do this, the library is
  28. not standard conforming any longer.  If the programmer decided to use this
  29. "feature", he needs a way to specify that he wants to open a file in text
  30. mode, and this way is "rt".  Now, "rt" is no longer equivalent to "r" as
  31. required by the standard, but this is irrelevant, because the library isn't
  32. standard conforming, anyway.
  33.  
  34. The net result is that although nobody (in his right mind) changes the
  35. default behaviour of the stdio library, people become confused WRT to
  36. the actual meaning of "r" and use "rt", just to be sure.
  37.  
  38. Needless to say, the above discussion applies to "wt" as well.
  39.  
  40. The story isn't over, however :-)  If "rt" is OK and portable anywhere,
  41. "rt+" is a different case: it is equivalent to "r+" on MSDOS, but on
  42. any other platform it is equivalent to "r", because the meaningless 't'
  43. _and_ all the characters following it are discarded.  So, the correct
  44. spelling, if portability is a concern, is "r+t".  This remark also
  45. applies to "r+b" vs "rb+": while the two versions are equivalent on any
  46. ANSI-conformant library, "r+b" will also work on pre-ANSI libraries
  47. (which don't "understand" the 'b'), "rb+" will be interpreted as "r" by
  48. such a library.  So, "r+b" is a little bit more portable than "rb+",
  49. which is a good reason to prefer it.
  50.  
  51. Dan
  52. --
  53. Dan Pop
  54. CERN, CN Division
  55. Email: danpop@mail.cern.ch 
  56. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  57.